package com.flower.salary.controller;

import com.flower.salary.common.utils.R;
import com.flower.salary.common.utils.SalaryException;
import com.flower.salary.domain.bo.AddUserBO;
import com.flower.salary.domain.bo.LoginForm;
import com.flower.salary.domain.bo.UserBanBO;
import com.flower.salary.domain.entity.SysUser;
import com.flower.salary.domain.vo.ListUserVO;
import com.flower.salary.domain.vo.SysUserVO;
import com.flower.salary.domain.vo.UserInfoVO;
import com.flower.salary.service.FileUpload;
import com.flower.salary.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-12-20
 */
@RestController
@RequestMapping("sys/system/sysUser")
public class SysUserController {



    @Autowired
    private SysUserService userService;

    @Autowired
    private FileUpload fileUpload;


    @PostMapping("/login")
    public R logon(@RequestBody LoginForm user){

        String token = userService.logon(user.getUsername(), user.getPassword());

        return R.ok("token",token);
    }

    @GetMapping("/hello")
    public R hello(){
        throw   new SalaryException("账号不存在或者密码错误");
    }


    @GetMapping("/info")
    public R info(String token){

        SysUserVO info = userService.info(token);

        return R.ok(info);
    }


    @PostMapping("/addUser")
    public R addUser(@RequestBody AddUserBO bo){
        userService.addUser(bo);

        return R.ok();
    }


    @GetMapping("/logout")
    public R logout(){



        return R.ok();
    }


    @GetMapping ("/list")
    public R list(){
        List<ListUserVO> listUserVOS = userService.selectList();
        return R.ok(listUserVOS);
    }


    @PostMapping("/ban")
    public R ban(@RequestBody UserBanBO banBO){
        userService.ban(banBO);
        return R.ok(banBO.getName() + "用户修改状态成功");
    }


    @PostMapping("/editPassword")
    public R editPassword(String token,String password){

        userService.editPassword(token, password);
        return R.ok("修改成功");
    }

    @PostMapping("/uploadImg")
    public R upload(MultipartFile file){
        if(file==null){
            System.out.println("文件上传失败");
        }


        String image = fileUpload.upload(file);


        return R.ok(image);
    }



    @GetMapping("/userInfo")
    public R userInfo(String token){

        UserInfoVO userInfoVO = userService.userInfo(token);

        return R.ok(userInfoVO);
    }


    @PostMapping("/updateUserInfo")
    public R updateUserInfo(@RequestBody UserInfoVO vo){


        userService.updateUserInfo(vo);

        return R.ok("修改成功");
    }




}
